/**
 * User: wangbin
 * Date: 2008-12-26
 */

package com.ht.power3d.geop;

import com.ht.visiomodel.Coordinate;
import com.ht.visiomodel.Polyline;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class SVGDataReader {
    double width;
    double height;

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public List<Polyline> read(InputStream in) {
        Stack<List<Polyline>> stack = new Stack<List<Polyline>>();
        List<Polyline> lines = new ArrayList<Polyline>();
        try {
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            XMLStreamReader reader = inputFactory.createXMLStreamReader(in);

            while (reader.hasNext()) {
                switch (reader.getEventType()) {
                    case XMLStreamReader.START_ELEMENT:
                        String name = reader.getLocalName();
                        if (name.equals("g") && (reader.getAttributeValue(null, "id").equals("_x2D_DT") || reader.getAttributeValue(null, "id").equals("provinces"))) {
                            stack.push(lines);
                        } else if (name.equals("svg")) {
                            width = Double.parseDouble(reader.getAttributeValue(null, "width"));
                            height = Double.parseDouble(reader.getAttributeValue(null, "height"));
                        } else if (name.equals("polyline")) {
                            if (!stack.empty()) {
                                List<Polyline> l = stack.peek();
                                Polyline polyline = new Polyline();
                                polyline.toPoints(reader.getAttributeValue(null, "points"));
                                l.add(polyline);
                            }
                        } else if (name.equals("line")) {
                            if (!stack.empty()) {
                                List<Polyline> l = stack.peek();
                                Polyline polyline = new Polyline();
                                Coordinate coord1 = new Coordinate(Double.parseDouble(reader.getAttributeValue(null, "x1")),
                                        Double.parseDouble(reader.getAttributeValue(null, "y1")));
                                Coordinate coord2 = new Coordinate(Double.parseDouble(reader.getAttributeValue(null, "x2")),
                                        Double.parseDouble(reader.getAttributeValue(null, "y2")));
                                polyline.getPointList().add(coord1);
                                polyline.getPointList().add(coord2);
                                l.add(polyline);
                            }
                        } else {
                            if (!stack.empty()) {
                                stack.pop();
                            }
                        }
                    default:
                        break;
                }
                reader.next();
            }
        } catch (XMLStreamException e) {
            e.printStackTrace();
        }
        return lines;
    }
}